﻿#include "FxFalling.h"
#include "cmath"

vector<Vector3> _oriPos;
FxFalling::FxFalling(Vector3 _origin)
	:ParticleManager(_origin)
{
	m_isDeath = false;
	m_fLife = 5000;
	m_size = Vector3(0.2, 0.2, 0);
	m_color = Color4(1.0f, 1.0f, 0.0f, 1.0f);
	m_position = Vector3(_origin.X, _origin.Y, _origin.Z);
	m_accelerator = Vector3(0, 0.001, 0);
	m_velocity = Vector3(0, 0, 0);
	m_listParticle = vector<Particle*>();
	//_origin = vector<MyVector*>();
	radius = 13;
	angle = 0;
	countTime = 0;
	Generate(1);
}


FxFalling::~FxFalling(void)
{
}
void FxFalling::InitParticle()
{
	float _fLife = m_fLife;

	Vector3 _position = Vector3(radius * (float)(rand()%200 - 100) / 100, 0, radius * (float)(rand()%200 - 100) / 100);
	
	
	Vector3 _acc = Vector3(0, -abs(m_accelerator.Y + (float)(rand()%200 - 100) / 100000), 0);
	//MyVector _acc = MyVector(0, -abs(m_accelerator.Y ), 0);
	
	//angle += 90;
	//if(angle >= 360) angle = 0;

	float _s = m_size.X + (float)(rand()%200 - 100) / 1000; //-0.1 --> 0.1
	Vector3 _size = Vector3(_s, _s, 0);

	m_listParticle.push_back(new Particle(_fLife, _size, _position, _acc, m_color));
	_oriPos.push_back(_position);
}

void FxFalling::Update(float _dt)
{

	for(int i = 0; i < m_listParticle.size(); i++)
	{
		
		if(countTime > 7 && m_fLife > 0)
		{
			InitParticle();
			countTime = 0;
		}	
		Particle *p;
		p = m_listParticle[i];

		if(p->m_color.A < 0)
		{
			m_listParticle.erase(m_listParticle.begin() + i);
			_oriPos.erase(_oriPos.begin() + i);
		}
		else
		{
			p->m_velocity.Y += (p->m_accelerator.Y * _dt);
			
			p->m_velocity.X = _oriPos[i].X +  sin(p->m_velocity.Y);

			p->m_position.X = p->m_velocity.X;
			p->m_position.Y = p->m_velocity.Y;

			p->m_color.A -= 0.0001 * _dt;
			p->m_color.R -= 0.00002 * _dt;
			p->m_color.B += 0.00005 * _dt;
			p->m_fCurLive += _dt;
		}


	}
	if(m_listParticle.size() == 0) m_isDeath = true;
	countTime+=_dt;
	//m_fLife -= 0.001 * _dt;
}
void FxFalling::Draw(float _posX, float _posY, float _posZ, 
					 float _viewX, float _viewY, float _viewZ)
{
	float angleY = 0, angleX = 0;
	Vector3 _vector = Vector3(_posX - _viewX, _posY - _viewY, _posZ - _viewZ);
	//goc hop boi tia nhin va truc thang dung
	angleY =  90 - atan(sqrt(pow(_vector.X, 2) + pow(_vector.Z, 2)) / _vector.Y) * 180 / 3.14;//y
	if (_posZ < _viewZ)
	{
		angleY = - angleY;
	}
	//goc xoay ngang của camera --> goc xoay cho particle
	angleX = - 180 + atan(_vector.X / _vector.Z) * 180 / 3.14;//x

	//angle2 =   asin(_vector.X / sqrt(pow(_vector.X, 2) + pow(_vector.Z, 2))) * 180 / 3.14;
	glPushMatrix();

	float max = _vector.X;
	if(_vector.Y > _vector.X)
		max = _vector.Y;
	if(_vector.Z > _vector.Y)
		max = _vector.Z;
	_vector.X = abs( 1 - (_vector.X / max));
	_vector.Y = abs( 1 - (_vector.Y / max));
	_vector.Z = abs( 1 - (_vector.Z / max));


	ResourceManager::GetInst()->LoadTexture("Resource/texture/particle.png");
	glDepthMask(false);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_TEXTURE_2D);

	glTranslatef(m_position.X, m_position.Y, m_position.Z);

	for(int i = 0; i < m_listParticle.size(); i++)
	{
		Particle *p;
		p = m_listParticle[i];


		glPushMatrix();

		glTranslatef(p->m_position.X, p->m_position.Y, p->m_position.Z);
		
		/*glRotatef(-50, 1, 0, 0);*/
		
		//glRotatef(angle, _vector.X, _vector.Y, _vector.Z);
		
		glRotatef(angleX, 0, 1, 0);
		glRotatef(angleY, 1, 0, 0);
		
		float _sizeX = p->m_size.X / 2;
		float _sizeY = p->m_size.Y / 2;
		glColor4f((GLfloat) p->m_color.R, (GLfloat) p->m_color.G, (GLfloat) p->m_color.B, (GLfloat) p->m_color.A);
		//glColor4f(1, 0, 0, p->m_color.A);
		glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-_sizeX, -_sizeY,  0);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( _sizeX, -_sizeY,  0);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( _sizeX,  _sizeY,  0);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-_sizeX,  _sizeY,  0);
		glEnd();

		glPopMatrix();


	}

	glDisable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glDisable(GL_TEXTURE_2D);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDepthMask(true);
	glPopMatrix();
}
